from django.db import models


class GoodsCategory(models.Model):
    """
    商品类别
    """
    CATEGORY_TYPE = (
        (1, "一级类目"),
        (2, "二级类目"),
        (3, "三级类目"),
    )

    category_name = models.CharField(default="", max_length=30, verbose_name="类别名", help_text="类别名")
    category_code = models.CharField(default="", max_length=30, verbose_name="类别代码", help_text="类别代码")
    category_level = models.IntegerField(choices=CATEGORY_TYPE, verbose_name="类别等级", help_text="类别等级")
    category_img = models.ImageField(upload_to="category/images/", null=True, blank=True, max_length=2083,
                                     verbose_name="类别图片", help_text="类别图片")
    parent_category = models.ForeignKey("self", null=True, blank=True, verbose_name="父类别", help_text="父类别",
                                        related_name="sub_cat", on_delete=models.CASCADE)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "商品类别"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.category_name


class Goods(models.Model):
    """
    商品
    """
    good_category = models.ForeignKey(GoodsCategory, verbose_name="商品类目", on_delete=models.CASCADE)
    good_sn = models.CharField(max_length=50, default="", verbose_name="商品唯一货号")
    good_name = models.CharField(max_length=100, verbose_name="商品名称")
    good_front_image = models.ImageField(upload_to="goods/images/", max_length=2083, null=True, blank=True,
                                         verbose_name="商品封面图")
    good_brief = models.TextField(max_length=500, verbose_name="商品简短描述")
    shop_price = models.FloatField(default=0, verbose_name="本店价格")
    market_price = models.FloatField(default=0, verbose_name="市场价格")
    stock_num = models.IntegerField(default=0, verbose_name="库存数量")
    sold_num = models.IntegerField(default=0, verbose_name="商品销售量")
    click_num = models.IntegerField(default=0, verbose_name="点击数")
    fav_num = models.IntegerField(default=0, verbose_name="收藏数")
    ship_free = models.BooleanField(default=True, verbose_name="是否承担运费")
    is_new = models.BooleanField(default=False, verbose_name="是否新品")
    is_hot = models.BooleanField(default=False, verbose_name="是否热销")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.good_name


class GoodsImage(models.Model):
    """
    商品轮播图
    """
    good = models.ForeignKey(Goods, verbose_name="商品", related_name="images", on_delete=models.CASCADE)
    image_id = models.CharField(max_length=50, default="", verbose_name="轮播图唯一标识")
    image_path = models.ImageField(upload_to="goods/swipers/", max_length=2083, null=True, blank=True,
                                   verbose_name="商品轮播图")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = '商品图片'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.good.good_name


class GoodsTags(models.Model):
    """
    商品标签
    """
    good = models.ForeignKey(Goods, verbose_name="商品", related_name="tags", on_delete=models.CASCADE)
    tag = models.CharField(max_length=50, default="", verbose_name="商品标签")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = '商品标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.good.good_name
